// -----------------------------------------------------------------------
//  <copyright file="WebApplicationExtensions.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/13 21:22</last-date>
// -----------------------------------------------------------------------

using DaprPlus.AspNetCore.Middleware;

using Microsoft.AspNetCore.Routing;


namespace DaprPlus.AspNetCore.Extensions;

public static class WebApplicationExtensions
{
    /// <summary>
    /// DaprPlus框架初始化，适用于Web应用程序
    /// </summary>
    public static Task<WebApplication> UseDaprPlus(this WebApplication app)
    {
        var builder = app.Services.GetRequiredService<IDaprPlusBuilder>();
        return builder.UseDaprPlus(app);
    }

    /// <summary>
    /// 添加Endpoint并Area路由支持
    /// </summary>
    public static IEndpointRouteBuilder MapControllerWithAreaRoute(this IEndpointRouteBuilder app, bool area = true)
    {
        if (area)
        {
            app.MapControllerRoute(
                name: "area-router",
                pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
            );
        }
        app.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}"
        );

        return app;
    }
}
